<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>组件通信-父子组件通信案例</title>
</head>
<body>
<div id="app">
  <cpn :number1="num1"
       :number2="num2"
       @number1change="number1Change"
       @number2change="number2Change"></cpn>
  <hr>
  <p>num1: {{num1}}</p>
  <p>num2: {{num2}}</p>
</div>

<template id="cpn">
  <div>
    <h2>prop:{{number1}}</h2>
    <h2>data:{{dnumber1}}</h2>
    <input type="text" :value="dnumber1" @input="number1Input">
    <h2>prop:{{number2}}</h2>
    <h2>data:{{dnumber2}}</h2>
    <input type="text" v-model="dnumber2" @input="number2Input">
  </div>
</template>

<script src="../js/vue.js"></script>
<script>
  const app = new Vue({
    el: '#app',
    data: {
      num1: 1,
      num2: 2
    },
    methods: {
      number1Change(value) {
        this.num1 = parseFloat(value);
      },
      number2Change(value) {
        this.num2 = parseFloat(value);
      }
    },
    components: {
      cpn: {
        template: '#cpn',
        data() {
          return {
            dnumber1: this.number1,
            dnumber2: this.number2
          }
        },
        props: {
          number1: Number,
          number2: Number
        },
        methods: {
          number1Input(event) {
            // 1、将input中的value赋值到dnumber中
            this.dnumber1 = event.target.value;
            // 2、发射自定义事件将dnumber中的值传递到父组件data中
            this.$emit('number1change', this.dnumber1);

            // 3、将dnumber1的值乘以100并赋值给dnumber2
            // this.dnumber2 = this.dnumber1 * 100;
            // this.$emit('number2change',this.dnumber2);
          },
          number2Input(event) {
            // this.dnumber2 = event.target.value;
            this.$emit('number2change', this.dnumber2);

            // this.dnumber1 = this.dnumber2 / 100;
            // this.$emit('number1change',this.dnumber1);
          }
        }
      }
    }
  });
</script>
</body>
</html>
